package com.sinothk.framework.eventbus.demo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.sinothk.framework.operateBus.OperateBus
import com.sinothk.framework.operateBus.Subscribe
import com.sinothk.framework.operateBus.ThreadMode
import com.sinothk.framework.operateBus.demo.R
import kotlinx.android.synthetic.main.activity_main.*

// https://github.com/greenrobot/EventBus

class MainActivity : AppCompatActivity() {

    private var userVM: UserViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        userVM = UserViewModel()

        sendTxtBtn.setOnClickListener {
            Thread {
                userVM!!.login("admin", "123456")
            }.start()
        }
    }

    @Subscribe(threadMode = ThreadMode.ASYNC)
    fun onShowEventMessage(messageEvent: MessageEvent) { //将数据显示到页面上
        tvMessage.text = messageEvent.message
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    fun onShowEventMessage2(messageEvent: MessageEvent2) { //将数据显示到页面上
        tvMessage2.text = messageEvent.message
    }

    override fun onStart() {
        super.onStart()

        //注册事件
        OperateBus.register(this)
    }

    override fun onStop() {
        super.onStop()
        OperateBus.unregister(this)
    }
}
